package site.isscloud.project.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import site.isscloud.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.isscloud.project.domain.PjLocationMarker;
import site.isscloud.project.mapper.PjLocationMapper;
import site.isscloud.project.domain.PjLocation;
import site.isscloud.project.mapper.PjLocationMarkerMapper;
import site.isscloud.project.service.IPjLocationService;

/**
 * 空间管理Service业务层处理
 * 
 * @author junhuang
 * @date 2025-10-14
 */
@Service
public class PjLocationServiceImpl implements IPjLocationService 
{
    @Autowired
    private PjLocationMapper pjLocationMapper;

    @Autowired
    private PjLocationMarkerMapper pjLocationMarkerMapper;

    /**
     * 查询空间管理
     * 
     * @param lcId 空间管理主键
     * @return 空间管理
     */
    @Override
    public PjLocation selectPjLocationByLcId(Long lcId)
    {
        return pjLocationMapper.selectPjLocationByLcId(lcId);
    }

    /**
     * 查询空间管理列表
     * 
     * @param pjLocation 空间管理
     * @return 空间管理
     */
    @Override
    public List<PjLocation> selectPjLocationList(PjLocation pjLocation)
    {
        return pjLocationMapper.selectPjLocationList(pjLocation);
    }

    /**
     * 新增空间管理
     * 
     * @param pjLocation 空间管理
     * @return 结果
     */
    @Override
    public int insertPjLocation(PjLocation pjLocation)
    {
        pjLocation.setCreateTime(DateUtils.getNowDate());
        return pjLocationMapper.insertPjLocation(pjLocation);
    }

    /**
     * 修改空间管理
     * 
     * @param pjLocation 空间管理
     * @return 结果
     */
    @Override
    public int updatePjLocation(PjLocation pjLocation)
    {
        pjLocation.setUpdateTime(DateUtils.getNowDate());
        return pjLocationMapper.updatePjLocation(pjLocation);
    }

    /**
     * 批量删除空间管理
     * 
     * @param lcIds 需要删除的空间管理主键
     * @return 结果
     */
    @Override
    public int deletePjLocationByLcIds(Long[] lcIds)
    {
        return pjLocationMapper.deletePjLocationByLcIds(lcIds);
    }

    /**
     * 删除空间管理信息
     * 
     * @param lcId 空间管理主键
     * @return 结果
     */
    @Override
    public int deletePjLocationByLcId(Long lcId)
    {
        return pjLocationMapper.deletePjLocationByLcId(lcId);
    }

    /**
     * 查找父空间ID
     */
    private Long findParentId(String parentCode, Map<String, Long> codeToIdMap, Map<String, Long> processedCodes) {
        if (parentCode == null || parentCode.isEmpty()) {
            return 0L; // 根节点
        }

        // 首先在数据库中查找
        Long parentId = codeToIdMap.get(parentCode);
        if (parentId != null) {
            return parentId;
        }

        // 然后在本次已处理的数据中查找
        parentId = processedCodes.get(parentCode);
        if (parentId != null) {
            return parentId;
        }

        // 如果都找不到，抛出异常
        throw new RuntimeException("父空间编码 " + parentCode + " 不存在");
    }

    /**
     * 通过空间编码查询空间信息
     */
    @Override
    public PjLocation selectPjLocationByLcCode(String lcCode) {
        return pjLocationMapper.selectPjLocationByLcCode(lcCode);
    }

    /**
     * 修改空间管理米标信息
     */
    @Override
    public int updatePjLocationMarker(PjLocation pjLocation) {
        // 判断是否存在 markerId，如果存在则更新，不存在则插入
        if (pjLocation.getMarkerId() != null && pjLocation.getMarkerId() > 0) {
            // 存在 markerId，执行更新操作
            // 需要将 PjLocation 转换为 PjLocationMarker
            PjLocationMarker marker = convertToMarker(pjLocation);
            return pjLocationMarkerMapper.updatePjLocationMarker(marker);
        } else {
            // 不存在 markerId，执行插入操作
            PjLocationMarker marker = convertToMarker(pjLocation);
            int result = pjLocationMarkerMapper.insertPjLocationMarker(marker);
            // 如果需要，可以将新生成的 markerId 设置回 pjLocation
            if (result > 0) {
                pjLocation.setMarkerId(marker.getId());
            }
            return result;
        }
    }

    /**
     * 将 PjLocation 对象转换为 PjLocationMarker 对象
     */
    private PjLocationMarker convertToMarker(PjLocation pjLocation) {
        PjLocationMarker marker = new PjLocationMarker();

        // 设置基本字段
        if (pjLocation.getMarkerId() != null) {
            marker.setId(pjLocation.getMarkerId());
        }
        marker.setLcCode(pjLocation.getLcCode());
        marker.setDeviceNo(pjLocation.getDeviceNo());
        marker.setChannelNo(pjLocation.getChannelNo());
        marker.setMarkerStart(pjLocation.getStartMeter());
        marker.setMarkerEnd(pjLocation.getEndMeter());

        // 计算米标长度
        if (pjLocation.getStartMeter() != null && pjLocation.getEndMeter() != null) {
            marker.setMarkerLength(pjLocation.getEndMeter() - pjLocation.getStartMeter());
        }

        // 设置状态，默认为正常
        marker.setStatus(1);

        // 设置操作人信息
        marker.setUpdateBy(pjLocation.getUpdateBy());
        marker.setUpdateTime(new Date());

        // 如果是新增操作，设置创建人信息
        if (pjLocation.getMarkerId() == null) {
            marker.setCreateBy(pjLocation.getCreateBy());
            marker.setCreateTime(new Date());
        }

        return marker;
    }
}
